package com.speechb.db;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
import java.io.IOException;
import java.io.InputStream;
/*
* Mybatis工具类
* 创建SqlSessionFactory对象以及获取SqlSession对象
* */
public class MyBatisUtils {
    private static SqlSessionFactory factory = null;
    static{

    }
    /*
    * @param autoCommit 设置是否为自动提交
    * */
    public static SqlSession getSession(String mapperName){
        InputStream is = null;
        try{
            is = Resources.getResourceAsStream(mapperName);
            SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(is);
            factory = sessionFactory;
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            if(is!=null)
                try{
                    is.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
        }
        return factory.openSession();
    }
}